<template>
  <div>
    <van-nav-bar title="账号登录" left-arrow />
    <van-form @submit="onSubmit" class="login-form">
      <van-field
        v-model="username"
        name="username"
        placeholder="请输入账号"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        placeholder="请输入密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button block type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>
    <div class="link" @click="$router.push('/register')">
      还没有账号？去注册
    </div>
  </div>
</template>

<script>
import { login } from '@/api/user'
import { setToken } from '@/utils/storage.js'
export default {
  data() {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit(values) {
      const { body } = await login(values)
      setToken(body.token)
      this.$toast.success('登录成功')
      this.$router.push('/layout')
    }
  }
}
</script>

<style scoped lang="less">
.login-form {
  margin-top: 20px;

  ::v-deep input {
    height: 30px;
  }

  ::v-deep .van-button__text {
    letter-spacing: 5px;
  }
}

.link {
  margin-top: 30px;
  font-size: 14px;
  text-align: center;
  color: #5bd3c7;
}
</style>
